package com.lxmes.gs.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.WorkingHoursStatisticsMapper;
import com.lxmes.gs.domain.WorkingHoursStatistics;
import com.lxmes.gs.service.IWorkingHoursStatisticsService;

/**
 * 工时统计Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class WorkingHoursStatisticsServiceImpl extends ServiceImpl<WorkingHoursStatisticsMapper,WorkingHoursStatistics> implements IWorkingHoursStatisticsService{
    
    @Autowired
    private WorkingHoursStatisticsMapper workingHoursStatisticsMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询工时统计
     * 
     * @param id 工时统计主键
     * @return 工时统计
     */
    @Override
    public WorkingHoursStatistics selectWorkingHoursStatisticsById(Long id){
        return workingHoursStatisticsMapper.selectById(id);
    }

    /**
     * 查询工时统计列表
     * 
     * @param workingHoursStatistics 工时统计
     * @return 工时统计
     */
    @Override
    public List<WorkingHoursStatistics> selectWorkingHoursStatisticsList(WorkingHoursStatistics workingHoursStatistics){
        return workingHoursStatisticsMapper.selectList(Wrappers.lambdaQuery(workingHoursStatistics));
    }

    /**
     * 新增工时统计
     * 
     * @param workingHoursStatistics 工时统计
     * @return 结果
     */
    @Override
    public int insertWorkingHoursStatistics(WorkingHoursStatistics workingHoursStatistics){
        return workingHoursStatisticsMapper.insert(workingHoursStatistics);
    }

    /**
     * 修改工时统计
     * 
     * @param workingHoursStatistics 工时统计
     * @return 结果
     */
    @Override
    public int updateWorkingHoursStatistics(WorkingHoursStatistics workingHoursStatistics){
        return workingHoursStatisticsMapper.updateById(workingHoursStatistics);
    }

    /**
     * 批量删除工时统计
     * 
     * @param ids 需要删除的工时统计主键
     * @return 结果
     */
    @Override
    public int deleteWorkingHoursStatisticsByIds(Long[] ids){
        return workingHoursStatisticsMapper.deleteWorkingHoursStatisticsByIds(ids);
    }

    /**
     * 删除工时统计信息
     * 
     * @param id 工时统计主键
     * @return 结果
     */
    @Override
    public int deleteWorkingHoursStatisticsById(Long id){
        return workingHoursStatisticsMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<WorkingHoursStatistics> workingHoursStatisticsList, boolean updateSupport) {
        if (StringUtils.isNull(workingHoursStatisticsList) || workingHoursStatisticsList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WorkingHoursStatistics workingHoursStatistics : workingHoursStatisticsList)
        {
            try
            {
                BeanValidators.validateWithException(validator, workingHoursStatistics);
                this.insertWorkingHoursStatistics(workingHoursStatistics);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
